#include "GLTexture.h"
GLTexture::GLTexture()
{
	activatedID = -1;
	ID = new vector<unsigned int>;
	strID = new vector<string>;
}



bool GLTexture::LOAD_BITMAP( GLBitmap *bitmap, string newStrID )
{
	_width = bitmap->getWidth();
	_height = bitmap->getHeight();

	if( newStrID == "" )
		return false;
		//throw "Exception in GLTexture::LOAD_BITMAP(...)# id String must have chars!";


	for( unsigned int i = 0;i < strID->size();i++ )
	{
		if( strID->at(i) == newStrID )
		{
			return false;
			//throw "Exception in GLTexture::LOAD_BITMAP(...)# id String allready exists!";
		}
	}

	strID->push_back( *new string(newStrID) );
	//cout<<"pushed"<<endl;
	unsigned int tmpID;

	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glGenTextures(1,&tmpID);
	//cout <<"GL_ERROR"<<glGetError()<<endl;
	glBindTexture( GL_TEXTURE_2D, tmpID);
	gluBuild2DMipmaps(	GL_TEXTURE_2D,
						3,
						_width,
						_height,
						GL_RGB,
						GL_UNSIGNED_BYTE,
						bitmap->getRGBBytes()
	);

	ID->push_back( tmpID );

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	delete bitmap;
	return true;
}

bool GLTexture::LOAD_Q3O_TEX( Q3O_TEXTURE *tex, string newStrID )
{
	try
	{
		_width = tex->textureWidth;
		_height = tex->textureHeight;
		//cout << _width<<endl;
		if( newStrID == "" )
			return false;
			//throw "Exception in GLTexture::LOAD_BITMAP(...)# id String must have chars!";

		//cout <<newStrID<<endl;
		for( unsigned int i = 0;i < strID->size();i++ )
		{
			//cout <<i<<endl;
			if( strID->at(i) == newStrID )
			{
				return false;
				//throw "Exception in GLTexture::LOAD_BITMAP(...)# id String allready exists!";
			}
		}
		strID->push_back( *new string(newStrID) );
		//cout <<"after pushed"<<endl;

		unsigned int *tmpID = new unsigned int;

		glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
		glGenTextures(1,tmpID);
		//~ cout <<"GL_ERROR"<<glGetError()<<endl;
		//~ cout << "GEN_TEX_ID: "<<*tmpID<<endl;
		glBindTexture( GL_TEXTURE_2D, *tmpID);
		//cout <<" ...after glBindTexture..."<<endl;

		/*for( int i = 0; i < _width*_height;i++ )
		{
			//cout <<"___PIXEL___# "<<i<<endl;
			//cout <<  *((unsigned char*)(tex->texture)+0)<<endl;
			//cout <<tex->texture[i]<<endl;
			//cout <<(int)tex->texture[i].color[1]<<endl;
			//cout <<(int)tex->texture[i].color[2]<<endl;
		}*/
		//~ cout <<" ...before gluBuild2DMipmaps..."<<endl;

		gluBuild2DMipmaps(	GL_TEXTURE_2D,
							3,
							_width,
							_height,
							GL_RGB,
							GL_UNSIGNED_BYTE,
							(unsigned char*)tex->texture
		);
		//~ cout <<" ...after gluBuild2DMipmaps..."<<endl;
		ID->push_back( *tmpID );

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
		//~ cout <<"Texture Binding OK"<<endl;
		return true;
	}
	catch( ... )
	{
		cout << "exception on load_q3o_texture!"<<endl;
		return false;
	}
}

bool GLTexture::ACTIVATE( string activeID )
{
	unsigned int hereIsTheID = 0;
	bool noID = true;
	for( unsigned int i = 0;i< strID->size() ;i++ )
		if( strID->at(i) == activeID )
		{
			hereIsTheID = i;
			noID = false;
			break;
		}
	//cout << "TEX_ID: "<<hereIsTheID<<endl;
	if( noID  )
		return false;

	if( hereIsTheID != activatedID )
		glBindTexture( GL_TEXTURE_2D, ID->at(hereIsTheID));
	return true;
}

bool GLTexture::DEACTIVATE(  )
{
	glBindTexture( GL_TEXTURE_2D, 0);
}

bool GLTexture::ACTIVATE( unsigned int activID )
{
	glBindTexture( GL_TEXTURE_2D, activID);
}


unsigned int GLTexture::getIntID( string id )
{
	unsigned int hereIsTheID = 0;
	bool noID = true;
	for( unsigned int i = 0;i< strID->size() ;i++ )
		if( strID->at(i) == id )
		{
			hereIsTheID = i;
			noID = false;
			break;
		}
	//cout << "TEX_ID: "<<hereIsTheID<<endl;
	if( noID  )
		return false;

	return ID->at(hereIsTheID);
}
